#ifndef __FT6336_H
#define __FT6336_H

#include <Arduino.h>
#include <Wire.h>


#define FT6336_SLAVE_ADDRESS    (0x38)
#define FT6336_MODE_REG         (0x00)
#define FT6336_TOUCHES_REG      (0x02)
#define FT6336_VENDID_REG       (0xA8)//FocalTech's Panel ID
#define FT6336_CHIPID_REG       (0xA3)//Chip Selecting
#define FT6336_THRESHHOLD_REG   (0x80)
#define FT6336_POWER_REG        (0x87)

#define FT6336_MONITOR          (0x01)
#define FT6336_SLEEP_IN         (0x03)

#define FT6336_VENDID           (0x11)
#define FT6336U_CHIPID          (0x64)

#define DEVIDE_MODE 0x00
#define TD_STATUS   0x02
#define TOUCH1_XH   0x03
#define TOUCH1_XL   0x04
#define TOUCH1_YH   0x05
#define TOUCH1_YL   0x06






class TP_Point
{
public:
    TP_Point(void)
    {
        x = 0;
        y = 0;
    }

    TP_Point(int16_t _x, int16_t _y)
    {
        x = _x;
        y = _y;
    }
    int16_t x;
    int16_t y;
};


class FT6336_Class
{
public:
    FT6336_Class() {};
    int begin(TwoWire &port = Wire, uint8_t addr = FT6336_SLAVE_ADDRESS);
    // valid touching detect threshold.
    void adjustTheshold(uint8_t thresh);
    TP_Point getPoint(uint8_t num = 0);
    uint8_t touched();
    void enterSleepMode();
    void enterMonitorMode();
private:
    void _readRegister();
    int _readByte(uint8_t reg, uint8_t nbytes, uint8_t *data)
    {
        _i2cPort->beginTransmission(_address);
        _i2cPort->write(reg);
        _i2cPort->endTransmission();
        _i2cPort->requestFrom(_address, nbytes);
        uint8_t index = 0;
        while (_i2cPort->available())
            data[index++] = _i2cPort->read();
    }

    int _writeByte(uint8_t reg, uint8_t nbytes, uint8_t *data)
    {
        _i2cPort->beginTransmission(_address);
        _i2cPort->write(reg);
        for (uint8_t i = 0; i < nbytes; i++) {
            _i2cPort->write(data[i]);
        }
        _i2cPort->endTransmission();
    }

    uint8_t _address;
    uint8_t _data[16];
    uint16_t _x[2];
    uint16_t _y[2];
    uint16_t _id[2];
    uint8_t _touches = 0;
    bool _init = false;
    TwoWire *_i2cPort;
};


#endif

